package com.chaoyi.util.schedule;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.chaoyi.util.schedule.job.StatJob;

public enum JobManager {
	INSTANCE;
	private final Logger logger = LoggerFactory.getLogger(JobManager.class);
	private final ScheduledExecutorSingleton scheduledExecutor = ScheduledExecutorSingleton.INSTANCE;
	private final ZoneOffset zoneOffset = ZoneOffset.ofHours(8);
	
	public void start(){
		LocalDateTime now = LocalDateTime.now();
		LocalDateTime temp = now.withHour(0).withMinute(30);
		long initialDelay = 0;
		if(now.isAfter(temp))
			temp = temp.plusDays(1);
		initialDelay = temp.toInstant(zoneOffset).getEpochSecond() - now.toInstant(zoneOffset).getEpochSecond();
		logger.info("开启定时任务,initialDelay="+initialDelay);
		scheduledExecutor.scheduleAtFixedRate(new StatJob(), initialDelay, 24*60*60, TimeUnit.SECONDS);
	}
	
	public void shutdown(){
		logger.info("关闭定时任务");
		scheduledExecutor.shutdown();
	}
	
}
